/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
//给定区间反转链表
class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
            if(head==null||left==right){
                return head;
            }
            //定义一个虚拟头结点
            ListNode dummy=new ListNode(-1);
            dummy.next=head;
            //定义一个prev,指向虚拟头结点y;
            ListNode prev=dummy;
            //定义一个cur,指向头结点
            ListNode cur=head;
            //遍历链表
            for(int i=0;i<left-1;i++){
               
                prev=prev.next; 
                cur=cur.next;
            }
            //做反转
           for(int i=0;i<right-left;i++){
               ListNode tmp=cur.next;
              cur.next=cur.next.next;
              tmp.next=prev.next;
              prev.next=tmp;


           }
           return dummy.next;

    }
}